﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using agent02.Model;

namespace agent02.Areas.Admin.Controllers
{
    public class AccountController : AdminBaseController
    {
        AgentDBEntities db = new AgentDBEntities();

        //
        // GET: /Admin/Account/
        
        public ActionResult Index()
        {
            var account = from t in db.TAI_KHOAN
                          join k in db.KHACH_HANG
                          on t.MaTaiKhoan equals k.MaTaiKhoan
                          select t;
            return View(account.ToList());
        }

        //
        // GET: /Admin/Account/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Admin/Account/Create

        [HttpPost]
        public ActionResult Create(TAI_KHOAN tk)
        {
            try
            {
                // TODO: add logic

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Admin/Account/Edit/5
 
        public ActionResult Edit(string id)
        {
            var acc = from a in db.TAI_KHOAN
                        where a.MaTaiKhoan == id
                        select a;
            if (acc.Count() == 1)
            {
                if (acc.Single().TrangThai == 1)
                {
                    acc.Single().TrangThai = 0;
                }
                else if (acc.Single().TrangThai == 0)
                {
                    acc.Single().TrangThai = 1;
                }

                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/Account/Delete/5
        public ActionResult Delete(string id)
        {
            var acc = from a in db.TAI_KHOAN
                      where a.MaTaiKhoan == id
                      select a;
            if (acc.Count() == 1)
            {
                return View(acc.Single());
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Admin/Account/Delete/5
        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            var acc = from a in db.TAI_KHOAN
                       where a.MaTaiKhoan == id
                       select a;
            if (acc.Count() == 1)
            {
                db.KHACH_HANG.DeleteObject(acc.Single().KHACH_HANG.Single());

                string mtk = acc.Single().MaTaiKhoan;
                var group = from g in db.TAIKHOAN_NHOM
                            where g.TaiKhoan == mtk
                            select g;
                foreach (var item in group)
                {
                    db.TAIKHOAN_NHOM.DeleteObject(item);
                }

                db.TAI_KHOAN.DeleteObject(acc.Single());

                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
    }
}
